package com.whu.graph.util;

public class SequentialIntClassArray {

	private int[] count;
	private String[] classes;
	private int size = 0;
	
	private int default_size = 8;
	
	public SequentialIntClassArray() {
		this.count = new int[default_size];
		this.classes=new String[default_size];
	}
	
	public SequentialIntClassArray(int default_size) {
		this.default_size = default_size;
		this.count = new int[default_size];
		this.classes=new String[default_size];
	}
	
	public SequentialIntClassArray(int size, int[] data,String[] cls) {
		this.count = data;
		this.size = size;
		this.classes= cls;
	}
	
	
	public int binarySearch(int target) {
		int low = 0;
		int mid = 0;
		int top = size - 1;
		
		while (low <= top) {
			mid = (low + top) / 2;
			if (target < count[mid]) {
				top = mid - 1;
			}
			else if (target > count[mid]) {
				low = mid + 1;
			}
			else if (target == count[mid]) {
				return mid;
			}
		}
		
		return -1;
	}
	
	public void insert(int num,String cls) {

		if (size == count.length) {
			
			int[] temp = count;
			count = new int[2 * size];
			for (int i = 0; i < temp.length; i++) {
				count[i] = temp[i];
			}
			
			String[] tm=classes;
			classes=new String[2*size];
			for(int i=0;i<tm.length;i++){
				classes[i]=tm[i];
			}
		}
		
		int low = 0;
		int mid = 0;
		int top = size - 1;
		
		while (low <= top) {
			mid = (low + top) / 2;
			if (num < count[mid]) {
				top = mid - 1;
			}
			else if (num > count[mid]) {
				low = mid + 1;
			}
			else if (num == count[mid]) {
				return;
			}
		}
		
		for (int i = size; i > low; i--) {
			count[i] = count[i - 1];
			classes[i]=classes[i-1];
		}
		count[low] = num;
		classes[low]=cls;
		
		size++;
	}
	
	public int getCapacity() {
		return size;
	}
	
	public int get(int index) {
		return count[index];
	}
	
	public String getClasses(int index){
		return classes[index];
	}
	
	public int getArrayLength() {
		return count.length;
	}
	
	public int[] getArray() {
		int[] result = new int[size];
		for (int i = 0; i < size; i++) {
			result[i] = count[i];
		}
		return result;
	}
	
	public String[] getAllClasses(){
		String[] cls = new String[size];
		for(int i=0;i<size;i++){
			cls[i]=classes[i];
		}
		return cls;
	}
	
	
//	public int getClsBycount(int data){
//		int dataIndex=-1;
//		if((dataIndex=binarySearch(data))!=-1){
//			return classes[dataIndex];
//		}
//		return -1;
//	}
	
	

	public static void main(String[] args){
//		SequentialIntDisArray vertices = new SequentialIntDisArray();
//		vertices.insert(11,2);
//		vertices.insert(11,3);
//		for(int x:vertices.getDistanceIndex()){
//			System.out.println(x);
//		}
		
	}

}
